//
//  PinPaiViewController.m
//  EJL
//
//  Created by adc on 15/11/5.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "PinPaiViewController.h"
#import "PPFXTableViewCell.h"
//#import "HttpManage.h"
//#import "PPXF.h"
#import "SouSuoJieGuoYe.h"
#import "HttpsManager.h"

#import "SouSuoTableViewCell.h"
#import "GoodsInfoVC.h"
#import "PPFXTableViewCell.h"
@interface PinPaiViewController ()

@end

@implementation PinPaiViewController

-(void)viewWillAppear:(BOOL)animated
{
    [GMDCircleLoader setOnView:self.view withTitle:nil animated:YES];
    [HttpsManager requestSouSuoJieGuo:_name tab:@"1" completion:^(SouSuoJieGuo *search)
     {
         if (search!=nil)
         {
             _sS=search;
             _dataAry=_sS.listAry;
             [_tableView reloadData];
             [GMDCircleLoader hideFromView:self.view animated:YES];
             
             
             __weak PinPaiViewController *weakSelf = self;
             [_tableView addPullToRefreshWithActionHandler:^{
                 NSLog(@"当进行下拉刷新执行该方法");
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
             
             [_tableView addInfiniteScrollingWithActionHandler:^{
                 [weakSelf performSelector:@selector(requestEnd) withObject:nil afterDelay:1.0f];
             }];
         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
             [GMDCircleLoader hideFromView:self.view animated:YES];
         }
        
     }];

    
    
    
}
- (void)requestEnd{
    [_tableView.pullToRefreshView stopAnimating];
    
    [_tableView.infiniteScrollingView stopAnimating];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"zzzz name  %@",_name);
    UIView*view=[[UIView alloc]initWithFrame:CGRectMake(0, 20,WIDE, 44)];
    view.backgroundColor=RGB_COLOR(0x00c49f);
    [self.view addSubview:view];
    UILabel*label=[[UILabel alloc]initWithFrame:CGRectMake(WIDE/2-75, 7, 150, 30)];
   label.text=@"搜索结果";
    label.textAlignment=NSTextAlignmentCenter;
    label.textColor=[UIColor whiteColor];
    [view addSubview:label];
    
    UIButton*backBut=[[UIButton alloc]initWithFrame:CGRectMake(10, 7, 30, 30)];
    [backBut setImage:[UIImage imageNamed:@"返回箭头"] forState:UIControlStateNormal];
    [backBut addTarget:self action:@selector(backBut) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:backBut];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 140, WIDE,HEIGHT-140) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.rowHeight=120;
    _tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:_tableView];
    //[_tableView registerNib:[UINib nibWithNibName:@"PinPaiFenXiaoTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [_tableView registerNib:[UINib nibWithNibName:@"PPFXTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    //    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)]) {
    //        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    //    }
    //    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
    //        [_tableView setSeparatorInset:UIEdgeInsetsZero];
    //    }
    
    for (int i=0; i<5; i++)
    {
        UIButton*pinPaiBut=[[UIButton alloc]initWithFrame:CGRectMake(0+WIDE/5*i, 64+44, WIDE/5, 44)];
        pinPaiBut.tag=i+1;
        NSArray*ary=[NSArray arrayWithObjects:@"默认", @"销量",@"价格",@"最新",@"佣金",nil];
        [pinPaiBut setTitle:ary[i] forState:UIControlStateNormal];
        [pinPaiBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [pinPaiBut setTitleColor:RGB_COLOR(0x00c49f)forState:UIControlStateSelected];
        if (i==0)
        {
            pinPaiBut.selected=YES;
            _select=pinPaiBut.tag;
            _a=0;
        }
        [pinPaiBut addTarget:self action:@selector(bigButClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:pinPaiBut];
        
    }
    
    
    
}


-(void)bigButClick:(UIButton*)sender
{
    UIButton*lastBut=(UIButton*)[self.view viewWithTag:_select];
    lastBut.selected=NO;
    
    sender.selected=YES;
    _select=sender.tag;
    _a=sender.tag-1;
    NSLog(@"----%d",_select);
    
    NSInteger index=sender.tag;
    
    
    NSString*str=[NSString stringWithFormat:@"%ld",(long)index];
//    [HttpsManager requestPinPaiFenXiao:str sub:[NSString stringWithFormat:@"%d",self.good.subId] completion:^(PinPaiFenXiao *pinPai)
//     {
//         _pinPai=pinPai;
//         _dataAry=_pinPai.listAry;
//         [_tableView reloadData];
//     }];
    
    [HttpsManager requestSouSuoJieGuo:_name tab:str completion:^(SouSuoJieGuo *search)
    {
        if (search!=nil)
        {
            _sS=search;
            _dataAry=_sS.listAry;
            [_tableView reloadData];
        }
        else
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }

    }];
    
    
}
-(void)backBut
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   // SecondPinPaiFenXiao*sec=_dataAry[indexPath.row];
    SouSuoJieGuoYe*SS=_dataAry[indexPath.row];
    GoodsInfoVC*vc=[[GoodsInfoVC alloc]init];
    
    vc.productId=SS.productId;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
    
}


- (IBAction)searchBut:(UIButton *)sender
{
    SearchViewController*vc=[[SearchViewController alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 120;
//}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    //    WoDeShangPinTableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"cell2" ];
    //    if (!cell) {
    //        cell = [[NSBundle mainBundle] loadNibNamed:@"WoDeShangPinTableViewCell" owner:nil options:nil][0];
    //    }
    //    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:sec.imgUrl] placeholderImage:[UIImage imageNamed:@"默认图片"]];
    //
    //
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    return cell;
    SouSuoJieGuoYe*sS=_dataAry[indexPath.row];
    PPFXTableViewCell*cell1=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    [cell1.picture sd_setImageWithURL:[NSURL URLWithString:sS.imgUrl] placeholderImage:[UIImage imageNamed:@"头像"]];
   
    cell1.name.text=sS.title;
    if (sS.discounting)
    {
        cell1.shouJia.text=[NSString stringWithFormat:@"%.2f",sS.price*0.01];
    }
    else
    {
        cell1.shouJia.text=[NSString stringWithFormat:@"%.2f",sS.discountPrice*0.01];
    }
    cell1.yongJin.text=[NSString stringWithFormat:@"%.2f",sS.commision*0.01];
    cell1.kuCun.text=[NSString stringWithFormat:@"%d",sS.collectionNum];
    return cell1;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//默认 销量－－最新 tag 0-4
- (IBAction)buttonClick:(UIButton *)sender
{
    
    
    
    
    
    
    
}
@end
